#!/usr/bin/env bash

# Prepare the system, this could run again and again.
echo "Upgrade prepare script"

# Ignore darwin
if [[ ! -d /usr/local/srs-cloud && $(uname -s) == 'Darwin' ]]; then
  echo "Directly finish upgrade for macOS development"
  sleep 3; exit 0;
fi

# Get the metadata of machine.
if [[ -f .env ]]; then source .env; fi
echo "LoadEnv CLOUD=$CLOUD REGION=$REGION, SOURCE=$SOURCE"

# Initialize the REGION and SOURCE, try to load from param 1.
# Note that we MUST load the REGION and SOURCE again, because nodejs executes this directly, and the region list might
# changed after the image is created.
if [[ $REGION == '' ]]; then REGION=$1; fi
bash auto/upgrade_region $SOURCE

# Update the sysctl
#   update_sysctl config value empty-line-prefix description
#   update_sysctl net.ipv4.ip_forward 1 0 "# Controls IP packet forwarding"
function update_sysctl() {
    SYSCTL_KEY=$1 && SYSCTL_VALUE=$2 && SYSCTL_EMPTY_LINE=$3 && SYSCTL_COMMENTS=$4
    echo "Update with sysctl $SYSCTL_KEY=$SYSCTL_VALUE, empty-line=$SYSCTL_EMPTY_LINE, comment=$SYSCTL_COMMENTS"

    grep -q "^${SYSCTL_KEY}[ ]*=" /etc/sysctl.conf
    if [[ $? == 0 ]]; then
      sed -i "s/^${SYSCTL_KEY}[ ]*=.*$/${SYSCTL_KEY} = ${SYSCTL_VALUE}/g" /etc/sysctl.conf
    else
      if [[ $SYSCTL_EMPTY_LINE == 1 ]]; then echo '' >> /etc/sysctl.conf; fi &&
      if [[ $SYSCTL_COMMENTS != '' ]]; then echo "$SYSCTL_COMMENTS" >> /etc/sysctl.conf; fi &&
      echo "${SYSCTL_KEY} = ${SYSCTL_VALUE}" >> /etc/sysctl.conf
    fi
    if [[ $? -ne 0 ]]; then echo "Failed to sysctl $SYSCTL_KEY = $SYSCTL_VALUE $SYSCTL_COMMENTS"; exit 1; fi

    RESULT=$(grep "^${SYSCTL_KEY}[ ]*=" /etc/sysctl.conf)
    echo "Update done: ${RESULT}"
}

# Setup the UDP kernel options before restart
# See https://www.jianshu.com/p/6d4a89359352
# TODO: FIXME: REMOVE-NEXT-RELEASE: Setup the kernel options by the image itself.
if [[ $(uname -s) != 'Darwin' ]]; then
  # 允许网络转发，Docker依赖这个配置
  # See https://stackoverflow.com/a/41453306/17679565
  update_sysctl net.ipv4.ip_forward 1 1 "# Controls IP packet forwarding"
  if [[ $? -ne 0 ]]; then echo "Setup the network forward failed"; exit 1; fi

  # Setup the UDP kernel buffer.
  # See https://www.jianshu.com/p/6d4a89359352
  update_sysctl net.core.rmem_max 16777216 1 "# For RTC/SRT over UDP"
  update_sysctl net.core.rmem_default 16777216
  update_sysctl net.core.wmem_max 16777216
  update_sysctl net.core.wmem_default 16777216
  if [[ $? -ne 0 ]]; then echo "Setup the UDP kernel options failed"; exit 1; fi
fi

# Setup the UDP kernel options for this session.
# See https://www.jianshu.com/p/6d4a89359352
if [[ $(uname -s) != 'Darwin' ]]; then
  if [[ -f /proc/sys/net/core/rmem_max ]]; then
    sysctl net.core.rmem_max=16777216 &&
    sysctl net.core.rmem_default=16777216 &&
    sysctl net.core.wmem_max=16777216 &&
    sysctl net.core.wmem_default=16777216
  fi
  if [[ $? -ne 0 ]]; then echo "Setup the UDP kernel options failed"; exit 1; fi
fi

# Use different docker registry.
REGISTRY=registry.cn-hangzhou.aliyuncs.com
if [[ $SOURCE == GITHUB || $SOURCE == github ]]; then REGISTRY=docker.io; fi
echo "Detect REGISTRY=$REGISTRY"

bash auto/upgrade_containers $REGISTRY
if [[ $? -ne 0 ]]; then echo "Update docker containers failed"; exit 1; fi

# Setup git alias to make it convenient.
# TODO: FIXME: Remove it because setup in image.
echo "Setup git alias to make it more convenient" &&
git config --local alias.co checkout &&
git config --local alias.br branch &&
git config --local alias.ci commit &&
git config --local alias.st status
if [[ $? -ne 0 ]]; then echo "Setup git alias failed"; exit 1; fi

# Setup nginx configuration.
bash auto/upgrade_nginx $CLOUD

# If systemctl config changed, reload it.
bash auto/upgrade_systemctl

# Execute script for each run.
bash auto/foreach_run
if [[ $? -ne 0 ]]; then echo "Execute for each run script failed"; exit 1; fi

